import { _decorator, Component, Node, SkeletalAnimation, Vec3, find, Camera } from "cc";
import event from "../../utils/event";
const { ccclass, property } = _decorator;

@ccclass("Prefabs/Game/Player")
export class Player extends Component {
    @property(Node)
    private role: Node = null;
    @property(Node)
    private uiMount: Node = null;

    public userId = "";
    public userName = "";
    public isMe = false;

    private roleAni: SkeletalAnimation = null;
    private action: string = "idle";
    private tempVec3: Vec3 = new Vec3();

    init(data, isMe) {
        this.userId = data.userId;
        this.userName = data.userName;
        this.isMe = isMe;
        this.roleAni = this.role.getComponent(SkeletalAnimation);
    }

    sync({ pos, euler, action }) {
        this.node.setWorldPosition(pos.x, pos.y, pos.z);
        this.node.setRotationFromEuler(euler.x, euler.y, euler.z);

        if (this.action !== action) {
            this.action = action;
            this.roleAni.play(action);
            console.log("role sync", action, this.userId, this.roleAni);
        }
    }

    getUIMountPos() {
        this.uiMount.getWorldPosition(this.tempVec3);
        return this.tempVec3.clone();
    }
    switchVisual(isShowRole: boolean) {
        this.role.active = isShowRole;
    }
}
